﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Configuration;

namespace NuaTarDoneng
{
    public partial class UeditorTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
                {
                    BTsubmit.Text = "提交更改";
                    string id = Request.QueryString["id"];
                    SqlConnection cnn = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
                    SqlCommand cmd = cnn.CreateCommand();
                    cmd.CommandText = "select * from [blogs] where blogID='" + id + "'";
                    cnn.Open();
                    SqlDataReader rd = cmd.ExecuteReader();
                    
                    while (rd.Read())
                    {
                        BlogID.Value = id;
                        titletb.Text = rd["blogTitle"].ToString();
                        myEditor.Text = System.Text.Encoding.Default.GetString(Convert.FromBase64String(rd["blogContent"].ToString()));
                        sortlist.SelectedValue = rd["SortID"].ToString();
                        keytb.Text = rd["keyword"].ToString();
                    }
                    cnn.Close();

                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (titletb.Text != "" && myEditor.Text != "" || keytb.Text != "")
            {
            string blogID = BlogID.Value;
            string blogTitle = titletb.Text;
            string blogContent = Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(myEditor.Text));
            string sortID = sortlist.SelectedValue;
            DateTime today = DateTime.Now;
            SqlConnection cnn = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
            SqlCommand cmd = cnn.CreateCommand();
            if (blogID == "")
            {
                blogID = Guid.NewGuid().ToString("N");
                cmd.CommandText = "insert into [blogs] (blogID,blogTitle,blogContent,blogTime,SortID,keyword) values ('"+blogID+"','"+blogTitle+"','"+blogContent+"','"+today+"','"+sortID+"','"+keytb.Text+"')";

            }
            else
            {
                cmd.CommandText = "update [blogs] set blogTitle='" + blogTitle + "',blogContent='" + blogContent + "',blogTime='" + today + "',sortID='" + sortID + "',keyword='" + keytb.Text + "' where blogID='" + blogID + "'";
            }
            cnn.Open();
            cmd.ExecuteNonQuery();
            cnn.Close();
            GenerMap.Mysitemap(this);
            Response.Redirect("~/BlogControl.aspx");
            }
        }
       
       
    }
}